﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace WindowsFormsApplication1
{
    class USBSensor : Sensor
    {
        private ManagementScope scope;
        private ManagementEventWatcher USBConnectWatch;
        private WqlEventQuery USBConnectQuery;
        private ManagementEventWatcher USBDisconnectWatch;
        private WqlEventQuery USBDisconnectQuery;
        //List<ManagementObject> usblist = new List<ManagementObject>();
        List<string> usblist = new List<string>();
        public USBSensor()
        {
            name = "USB Sensor";
            scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;
            USBConnectWatch = null;
            USBConnectQuery = new WqlEventQuery();
            USBDisconnectWatch = null;
            USBDisconnectQuery = new WqlEventQuery();
            try
            {
                initialize_queries();
                USBConnectWatch.Start();
                USBDisconnectWatch.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            populate();
        }
        private void initialize_queries()
        {
            USBConnectQuery.EventClassName = "__InstanceCreationEvent";
            USBConnectQuery.WithinInterval = new TimeSpan(0, 0, 10);
            USBConnectQuery.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice'";

            USBDisconnectQuery.EventClassName = "__InstanceDeletionEvent";
            USBDisconnectQuery.WithinInterval = new TimeSpan(0, 0, 10);
            USBDisconnectQuery.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice'";

            USBConnectWatch = new ManagementEventWatcher(scope, USBConnectQuery);
            USBConnectWatch.EventArrived += new EventArrivedEventHandler(USBEventArrived);

            USBDisconnectWatch = new ManagementEventWatcher(scope, USBDisconnectQuery);
            USBDisconnectWatch.EventArrived += new EventArrivedEventHandler(USBEventArrived);

        }
        public void setlist()
        {
            populate();
            Notify();
        }
        private void populate()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
                      "SELECT * FROM Win32_USBControllerDevice");

            foreach (ManagementObject queryobj in searcher.Get())
            {
                ManagementObject mbo = new ManagementObject(queryobj["Dependent"].ToString());
                string device="";
                foreach (PropertyData prop in mbo.Properties)
                {
                    if (prop.Name == "Caption") device += prop.Value.ToString() + " / ";// usblist.Add(prop.Value.ToString());
                    else if (prop.Name == "PNPDeviceID") device += prop.Value.ToString();
                }
                usblist.Add(device);
                //usblist.Add(queryobj.ToString());
            }
            data = usblist;
            //data = "USB Event Detected";
        }
        public void USBEventArrived(object sender, EventArrivedEventArgs e)
        {
            populate();
            Notify();
        }
    }
}
